<?php
namespace App\Services\Wxxcx;

use Illuminate\Support\Facades\DB;

class FuwuService extends BaseService
{
    /**
     * 服务
     */

    /**
     * 列表
     * @param array $pages【start，limit】
     */
    public function getList($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbFuwu)->where('del', 1)->where('type', '>', 0);
        if (isset($pas['uid']) && $pas['uid']) {
            $db = $db->where('uid', $pas['uid']);
        }
        $total = $db->count();
        $res = $db->orderBy('sort', 'asc')
            ->skip($start)->take($pages['limit'])
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $types = $this->fuwu_types;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $type_name = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'type' => $v['type'],
                'type_name' => $type_name,
                'cnt' => $v['cnt'],
                'prefixUrl' => env('DOMAIN').'/',
                'thumb' => file_exists($v['thumb']) ? $v['thumb'] : '',
                'price' => toNumber($v['price']),
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 家政日常
     */
    public function jzday()
    {
        $res = $this->dbConn($this->dbFwDay)
            ->where('del', 1)
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $imgOnes = $this->dbConn($this->dbFwImg)
                ->where('del', 1)
                ->where('day', $v['title'])
                ->orderBy('id', 'desc')
                ->skip(0)->take(2)
                ->get();
            $img01 = '';
            $img02 = '';
            if (count($imgOnes)) {
                $img01 = env('DOMAIN').'/'.$imgOnes[0]->img;
            }
            if (count($imgOnes) > 1) {
                $img02 = env('DOMAIN').'/'.$imgOnes[1]->img;
            }
            $arys[] = [
                'id' => $v['id'],
                'day' => $v['title'],
                'img01' => $img01,
                'img02' => $img02,
            ];
        }
        return $this->toback(0, '', $arys);
    }

    /**
     * 家政图片
     */
    public function jzimg($day)
    {
        $res = $this->dbConn($this->dbFwImg)
            ->where('del', 1)
            ->where('day', $day)
            ->orderBy('id', 'desc')
            //->skip(0)->take(100)
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'img' => env('DOMAIN').'/'.$v['img']
            ];
        }
        return $this->toback(0, '', $arys);
    }
}